﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace GaiNo.Core.Helpers
{
    public class RegexHelper
    {
        private const string _EMAIL = "\\w{1,}@\\w{1,}\\.\\w{1,}";
        private const string _NUM = "^[0-9]*$ ";
        private static bool Check(CheckType type, string inputStr)
        {
            RegexOptions options = ((RegexOptions.IgnorePatternWhitespace | RegexOptions.Multiline) | RegexOptions.IgnoreCase);

            Regex reg = null;
            switch (type)
            {
                case CheckType.EMAIL:
                    reg = new Regex(_EMAIL, options);
                    break;
                case CheckType.NUM:
                    reg = new Regex(_NUM, options);
                    break;
            }
            if (reg == null)
                return false;

            return reg.IsMatch(inputStr);
        }
        public static bool IsEmail(string input)
        {
            return Check(CheckType.EMAIL, input);
        }
        public static bool IsNum(string input)
        {
            return Check(CheckType.NUM, input);
        }
        public static string GetUrlParam(string query, string arg)
        {
            query = query.ToLower();
            arg = arg.ToLower();
            string rst = query;
            Regex re = new Regex(@"(?:^|\?|&)" + arg + "=([^&]*)(?:&|$)");
            if (re.IsMatch(query))
            {
                rst = re.Match(query).Groups[1].ToString();
            }
            return rst;
        }
    }
    enum CheckType
    {
        EMAIL,
        NUM,
    }
}
